package FIT::Document::t::Transformer;

use FIT::Test;
use FIT::Document::Transformer;
use FIT::Document::Transformer::Right;
use XML::Twig;

sub cell_is_right_low_level_interface: Test {
    my $cell = make_element();
    my $iut = FIT::Document::Transformer::Right->new;
    $iut->_transform($cell);
    is $cell->sprint, '<td class="fit_right">foo</td>';
}

sub cell_is_right: Test {
    my $cell = make_element();
    transform { Right } $cell;
    is $cell->sprint, '<td class="fit_right">foo</td>';
}

sub cell_is_skip: Test {
    my $cell = make_element();
    transform { Skip } $cell;
    is $cell->sprint, '<td class="fit_skip">foo</td>';
}

sub cell_is_void: Test {
    my $cell = make_element();
    transform { Void } $cell;
    is $cell->sprint, '<td>foo</td>';
}

sub cell_is_wrong: Test(4) {
    my $cell = make_element();
    transform { Wrong actual was 'bar' } $cell;
    my $html = $cell->sprint;
    like $html, qr/foo/, 'expected';
    like $html, qr/bar/, 'actual';
    like $html, qr/fit_wrong_note/, 'fit_wrong_note';
    like $html, qr/fit_wrong/, 'fit_wrong';
}

sub cell_is_error: Test(4) {
    my $cell = make_element();
    transform { Error description was 'bar' } $cell;
    my $html = $cell->sprint;
    like $html, qr/foo/, 'expected';
    like $html, qr/bar/, 'description';
    like $html, qr/fit_error/, 'fit_wrong_note';
    like $html, qr/<hr\/>/, 'hr';
}

sub good_attempt: Test {
    my $cell = make_element();
    attempt { 1 } $cell;
    is $cell->sprint, '<td>foo</td>';
}

sub bad_attempt: Test {
    my $cell = make_element();
    attempt { die 'baz' } $cell;
    like $cell->sprint, qr/baz/;
}

sub add_rows: Test {
    my $table = XML::Twig::Elt->new('table');
    $table->set_inner_xml('<tr><td>foo</td></tr>');
    transform { AddRows rows are [
        [qw(a b c)],
    ] } $table;
    is $table->sprint, '<table><tbody><tr><td colspan="3">foo</td></tr><tr><td>a</td><td>b</td><td>c</td></tr></tbody></table>';
}

sub make_element() {
    my $cell = XML::Twig::Elt->new('td');
    $cell->set_text('foo');
    return $cell;
}

